package com.hxy.health.platform.controller;

import com.hxy.health.platform.common.Const;
import com.hxy.health.platform.pojo.Cart;
import com.hxy.health.platform.pojo.Order;
import com.hxy.health.platform.pojo.User;
import com.hxy.health.platform.service.CartService;
import com.hxy.health.platform.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class PayController {
    @Autowired
    private CartService cartService;
    @Autowired
    private OrderService orderService;

    /**
     * 支付功能
     */
    @RequestMapping("page/paySuccessPage")
    public String paySuccessPage(HttpServletRequest request, Order order) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return "loginOrRegister";
        }
        // 去数据库查询购物车明细
        List<Cart> cartList = cartService.getCartByEmail(user.getEmail());
        int result = orderService.addOrder(cartList, order);
        if (result > 0) {
            // 清空购物车
            cartService.deleteByEmail(user.getEmail());
            return "index";
        }
        return "";
    }



}
